package com.xuxueli.crawler.test;

import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import com.xuxueli.crawler.conf.XxlCrawlerConf;
import com.xuxueli.crawler.parser.PageParser;
import com.xuxueli.crawler.proxy.ProxyMaker;
import com.xuxueli.crawler.proxy.strategy.RoundProxyMaker;
import com.xuxueli.crawler.test.util.HibernateUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;

/*
 *  lcx 爬取所有类目
 * */
public class AllCategories {

    //探测 最大分页的实体
    @PageSelect(cssQuery = "body")
    public static class MaxPageVo {


        @PageFieldSelect(cssQuery = ".srp-pagination > li > a", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> pageNumberList;

        @PageFieldSelect(cssQuery = ".warning > .clearMargin-bottom > li", selectType = XxlCrawlerConf.SelectType.TEXT)
        private String endState;

        public String getEndState() {
            return endState;
        }

        public void setEndState(String endState) {
            this.endState = endState;
        }

        public List<String> getPageNumberList() {
            return pageNumberList;
        }

        public void setPageNumberList(List<String> pageNumberList) {
            this.pageNumberList = pageNumberList;
        }

        @Override
        public String toString() {
            return "MaxPageVo{" +
                    "pageNumberList='" + pageNumberList + '}';
        }
    }


    @PageSelect(cssQuery = "body")
    public static class PageVo {


        @PageFieldSelect(cssQuery = ".sub-shop-list > li > a", selectType = XxlCrawlerConf.SelectType.TEXT)
        private List<String> shopNameList;

        @PageFieldSelect(cssQuery = ".sub-shop-list > li > a", selectType = XxlCrawlerConf.SelectType.ATTR, selectVal = "href")
        private List<String> shopNameListHref;


        public List<String> getShopNameList() {
            return shopNameList;
        }

        public void setShopNameList(List<String> shopNameList) {
            this.shopNameList = shopNameList;
        }

        public List<String> getShopNameListHref() {
            return shopNameListHref;
        }

        public void setShopNameListHref(List<String> shopNameListHref) {
            this.shopNameListHref = shopNameListHref;
        }

        @Override
        public String toString() {
            return "PageVo{" +
                    "shopNameList='" + shopNameList + '\'' +
                    ", shopNameListHref=" + shopNameListHref + '}';
        }
    }

    public static void main(String[] args) {

        String[] ip = {
                "222.85.39.159:38939",
                "49.85.5.17:28885"
        };

        // 设置代理池
        ProxyMaker proxyMaker = new RoundProxyMaker()
                .addProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("222.85.39.159", 38939)));


        final String[] link = {
                "https://uae.souq.com/ae-en/shop-all-categories/c/"
        };
        HibernateUtil hibernateUtil = new HibernateUtil();

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(link)
                .setThreadCount(1).setPauseMillis(1000)
                .setAllowSpread(false)
                //.setProxyMaker(proxyMaker)
                .setPageParser(
                        new PageParser<PageVo>() {
                            int i = 0;

                            @Override
                            public boolean parse(Document html, Element pageVoElement, PageVo pageVo) {
                                // 解析封装 PageVo 对象
                                String pageUrl = link[i];
                                System.out.println("---------------------------------------------------------");
                                System.out.println("第：" + i);
                                System.out.println("---------------------------------------------------------");
                                //探测当前分类下的最大分页数量
                                i++;

                                return true;
                            }

                        }
                )
                .build();

        System.out.println("start");
        crawler.start(true);

        hibernateUtil.saveCategory((PageVo)crawler.getPageVo());

        System.out.println("end");
    }

    public int findMaxPageNumber(String href) {

        //开始基准数字
        int maxPageNum = 128;

        //二分法缩小基准数字
        int dtPageNum = 0;

        //上一次的基准
        int lastmaxPageNum = 0;

        //上一次状态数量
        boolean state = false;

        String lastHref = href;

        do {

            href = lastHref + "?page=" + maxPageNum;

            XxlCrawler crawler1 = new XxlCrawler.Builder()
                    .setUrls(href)
                    .setThreadCount(1).setPauseMillis(1000)
                    .setAllowSpread(false)
                    //.setProxyMaker(proxyMaker)
                    .setPageParser(
                            new PageParser<MaxPageVo>() {
                                @Override
                                public boolean parse(Document html, Element pageVoElement, MaxPageVo maxPageVo) {
                                    // 解析封装 PageVo 对象
                                    String pageUrl = html.baseUri();
                                    System.out.println(pageUrl + "：" + maxPageVo.toString());

                                    //maxPageNum 太大了 告诉外部函数 需要缩小范围
                                    if (maxPageVo.getEndState() != null) {
                                        return true;
                                    }
                                    System.out.println("还有页数！");
                                    return false;
                                }
                            }
                    )
                    .build();
            crawler1.start(true);

            //上次最大页
            lastmaxPageNum = maxPageNum;


            //第一次进入加分运算的时候记录 二分法缩小基准数字
            if (dtPageNum == 0) {
                dtPageNum = maxPageNum / 2;
            }
            //上次的结果 如果等于这次的结果  那么 变化基数不变
            else if (state == crawler1.isRunState()) {
                dtPageNum = dtPageNum / 2;
            } else {
                dtPageNum = dtPageNum / 2;
            }

            //如果上次的结果是true 说明还有分页 则缩小范围
            state = crawler1.isRunState();
            if (state) {
                maxPageNum = maxPageNum - dtPageNum;
            } else {
                maxPageNum = maxPageNum + dtPageNum;
            }

            //如果上次的结果和这次相差一次说明
            if (Math.abs(maxPageNum - lastmaxPageNum) == 1 && state == true) {
                return  lastmaxPageNum;
            }
            if (Math.abs(maxPageNum - lastmaxPageNum) == 1 && state == false) {
                return maxPageNum;
            }

            System.out.println("下一次测试的最大页数是：" + maxPageNum + "---------dtPageNum:" + dtPageNum);



        }
        while (true);
    }


}
